package guoanmaker.operator.business.controller;

import guoanmaker.operator.business.model.OperatorCity;
import guoanmaker.operator.business.service.OperatorCityServiceInterface;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 *  
 *
 * @author huZhaoyu 
 * @ClassName: OperatorCityController
 * @Description: OperatorCityController 
 * @date 2017/6/29 上午10:11 
 */
@RestController
@RequestMapping(ApiController.OPERATOR_CITY)
public class OperatorCityController {
    private final AjaxJson ajaxJson;
    private final OperatorCityServiceInterface operatorCityServiceInterface;

    public OperatorCityController(AjaxJson ajaxJson, OperatorCityServiceInterface operatorCityServiceInterface) {
        this.ajaxJson = ajaxJson;
        this.operatorCityServiceInterface = operatorCityServiceInterface;
    }

    /** 
    * @Title: saveCity
    * @Description: 新增/修改开通城市 
    * @param: OperatorCity   
    * @return AjaxJson    
    * @throws 
    */
    @ApiOperation(value = "保存开通城市", notes = "新增开通城市")
    @PostMapping(value = "saveCity")
    public AjaxJson saveCity(String code) {
    	try {
    		int a=operatorCityServiceInterface.saveCity(code);
    		if(a==-2){
    			ajaxJson.setSuccess(true).setStatus(230).setData("不能删除已有商品或空间的城市");
    		}
    		else{
    			ajaxJson.setSuccess(true).setStatus(200).setData("保存成功");
    		}
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(250).setData("保存失败");
		}
        return ajaxJson;
    }
    @ApiOperation(value = "验证城市是否使用", notes = "验证城市是否使用")
    @PostMapping(value = "validateCity")
    public AjaxJson validateCity(String code) {
    	try {
    		int a=operatorCityServiceInterface.validateCity(code);
    		if(a==-1){
    			ajaxJson.setSuccess(true).setStatus(230).setData("该城市已存在商品或空间");
    		}
    		else{
    			ajaxJson.setSuccess(true).setStatus(200).setData("验证成功");
    		}
    	} catch (Exception e) {
    		e.printStackTrace();
    		ajaxJson.setSuccess(true).setStatus(250).setData("验证失败");
    	}
    	return ajaxJson;
    }

    /** 
     * @Title: deleteCityById
     * @Description: 根据Id删除开通城市
     * @param: String id  
     * @return AjaxJson 
     * @throws 
     */
    @ApiOperation(value = "删除开通城市", notes = "根据Id删除开通城市")
    @DeleteMapping(value = "delete/{id}")
    public AjaxJson deleteCityById(@PathVariable(value = "id") String id) {
        operatorCityServiceInterface.deleteCityById(id);
        return ajaxJson.setSuccess(true).setStatus(200).setData("数据删除成功!");
    }

    /**
     * @return OperatorCity    
     * @Title: getCityById 
     * @Description: 根据Id查询开通城市
     * @param: String id   
     * @throws 
     */
    @ApiOperation(value = "根据Id查询开通城市", notes = "根据Id查询开通城市")
    @GetMapping(value = "getCityById")
    public AjaxJson getCityById(@RequestParam(value = "id") String id) {
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorCityServiceInterface.getCityById(id));
    }

    /**
     * @return AjaxJson   
     * @Title: getAllCity
     * @Description: 查找所有开通城市 
     * @throws 
     */
    @ApiOperation(value = "查找所有开通城市", notes = "查找所有开通城市")
    @PostMapping(value = "getAllCity")
    public AjaxJson getAllCity() {
    	try {
    		ajaxJson.setSuccess(true).setStatus(200).setData(operatorCityServiceInterface.getAllCity());
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(250).setData("查询失败");
		}
        return ajaxJson;
    }

    /**
     * @return AjaxJson   
     * @Title: getAllCityByPage 
     * @Description: 查询所有开通城市并分页
     * @param: Integer page 页码
     * @param: Integer size 条数
     * @throws 
     */
    @ApiOperation(value = "分页查询开通城市", notes = "分页查询开通城市")
    @GetMapping(value = "getAllCityByPage")
    public AjaxJson getAllCityByPage(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                           @RequestParam(value = "size", defaultValue = "10") Integer size) {
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorCityServiceInterface.getAllCityByPage(page, size));
    }

    /**
     * @return AjaxJson   
     * @Title: getCityByPageOrderByWeight 
     * @Description: 按权重排序查询开通城市并分页 
     * @param: Integer page 页码
     * @param: Integer size 条数
     * @throws 
     */
    @ApiOperation(value = "按权重排序查询开通城市并分页", notes = "按权重排序查询开通城市并分页")
    @GetMapping(value = "getCityOrderByWeight")
    public AjaxJson getCityByPageOrderByWeight(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                             @RequestParam(value = "size", defaultValue = "10") Integer size) {
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorCityServiceInterface.getCityByPageOrderByWeight(page, size));
    }

    /**
     * @return AjaxJson   
     * @Title: getCityByStatus 
     * @Description: 根据状态查询城市 
     * @param: Integer status
     * @throws 
     */
    @ApiOperation(value = "根据状态查询城市", notes = "根据状态查询城市")
    @GetMapping(value = "getCityByStatus")
    public AjaxJson getCityByStatus(@RequestParam(value = "status") Integer status) {
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorCityServiceInterface.getCityByStatus(status)).setMsg("请求成功");
    }

    /**
     * @return AjaxJson   
     * @Title: getByCityId 
     * @Description: 根据城市ID查询城市 
     * @param: String cityId
     * @throws 
     */
    @ApiOperation(value = "根据城市ID查询城市", notes = "根据城市ID查询城市")
    @GetMapping(value = "getByCityId")
    public AjaxJson getCityByCityId(@RequestParam(value = "status") String cityId) {
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorCityServiceInterface.getByCityId(cityId)).setMsg("请求成功");
    }

    @ApiOperation(value = "APP接口", notes = "APP接口")
    @GetMapping(value = "getCity2App")
    public AjaxJson getCity2App(@RequestParam(value = "cityName") String cityName,@RequestParam(value = "username") String username,@RequestParam(value = "landType") String landType) {
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorCityServiceInterface.getCity2App(cityName,username,landType)).setMsg("请求成功");
    }
    
    @ApiOperation(value = "查询所有城市", notes = "查询所有城市")
    @PostMapping(value = "selectAllArea")
    public AjaxJson selectAllArea() {
    	try {
    		ajaxJson.setSuccess(true).setStatus(200).setData(operatorCityServiceInterface.selectAllArea());
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(250).setData("查询失败");
		}
    	return ajaxJson;
    }
    @ApiOperation(value = "根据省code查询城市", notes = "根据省code查询城市")
    @PostMapping(value = "selectAreaByCode")
    public AjaxJson selectAreaByCode(String code) {
    	try {
    		ajaxJson.setSuccess(true).setStatus(200).setData(operatorCityServiceInterface.selectAreaByCode(code));
    	} catch (Exception e) {
    		e.printStackTrace();
    		ajaxJson.setSuccess(true).setStatus(250).setData("查询失败");
    	}
    	return ajaxJson;
    }
    @ApiOperation(value = "查询所有省", notes = "查询所有省")
    @PostMapping(value = "selectAllPriovince")
    public AjaxJson selectAllPriovince() {
    	try {
    		ajaxJson.setSuccess(true).setStatus(200).setData(operatorCityServiceInterface.selectAllPriovince());
    	} catch (Exception e) {
    		e.printStackTrace();
    		ajaxJson.setSuccess(true).setStatus(250).setData("查询失败");
    	}
    	return ajaxJson;
    }
    
    @ApiOperation(value = "查找所有开通城市App首页", notes = "查找所有开通城市App首页")
    @PostMapping(value = "getAllCityApp")
    public AjaxJson getAllCityApp() {
    	try {
    		ajaxJson.setSuccess(true).setStatus(200).setData(operatorCityServiceInterface.selectCityforPc());
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(250).setData("查询失败");
		}
        return ajaxJson;
    }
    
    
}
